<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Enclosure as Enclosures;

/**
 * 原始单据接口
 */
class Enclosure extends Common
{
    protected $noNeedRight = '*';

    /**
     * 获取原始单据
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 原始单据名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $where = [];
            $bill_time = $this->request->post('bill_time');
            if(!empty($bill_time)){
                $str = explode('~',$bill_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['createtime'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $upload_date = $this->request->post('upload_date');
            if(!empty($upload_date)){
                $where['upload_date'] = ["like", '%'.$upload_date.'%'];
            }
            $type = $this->request->post('type');
            if(!empty($type)){
                $where['type'] = $type;
            }
            $ids = $this->request->post('ids');
            if(!empty($ids)){
                $where['id'] = ['in',$ids];
            }
            $list_type = $this->request->post('list_type');
            $list_type = $list_type==1?1:2;

            $sort_type = $this->request->post('sort_type');
            $sort_type = $sort_type==1?'DESC':'ASC';

            $status_type = $this->request->post('status_type');

            $result = Enclosures::getList($where,$page,$limit,$list_type,$sort_type,$status_type);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加原始单据
     * @ApiMethod (POST)
     * @param array $detail 原始单据数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Enclosures::addDetail($data);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除原始单据
     * @ApiMethod (POST)
     * @param int $id 原始单据ID
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = Enclosures::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
